﻿using UnityEngine;
using System.Collections;

public class EventTimer : MonoBehaviour {

	bool bDestroyOnce = true;
	bool bBrinkTrigger = true;

	int iSec = 30;  // 트럭 주차 시간
	int iTick = 0;
	// Use this for initialization
	void Start () {
		StartCoroutine (Timer());
		transform.localScale = new Vector3 (0.1f, 0.1f);
		transform.localPosition = new Vector3(0, 25.0f);
		GetComponent<UILabel> ().depth = 94;
	}
	
	// Update is called once per frame
	void Update () {

		if (bBrinkTrigger && iSec < 10) {
			bBrinkTrigger = false;
			GetComponent<TweenColor>().enabled = true;
		}

		if(iTick < 10)
			GetComponent<UILabel> ().text = string.Format ("{0} : 0{1}", iSec, iTick);
		else
			GetComponent<UILabel> ().text = string.Format ("{0} : {1}", iSec, iTick);

		if (iTick == 0 && iSec == 0 && bDestroyOnce) {
			GetComponent<UILabel> ().color = Color.red;
			StartCoroutine (DestroyThis ());
			GetComponent<TweenColor>().enabled = false;
			GetComponent<TweenAlpha>().enabled = true;
			bDestroyOnce = false;
            GameObject.Find("SupplyTruckObj(Clone)").GetComponent<SupplyTruckObj>().m_bShopingEnd = true;
			GameObject.Find("ShopBtn(Clone)").GetComponent<ShopBtn>().DelThis();
            if (GameObject.Find("ShopPanel").transform.childCount != 0)
				GameObject.Find("ShopPanel").transform.GetChild(0).GetComponent<ShopWdw>().DelThis();
		}
	}

	IEnumerator Timer(){
		yield return new WaitForSeconds(0.01f);
		if (iTick <= 0) {
			--iSec;
			iTick = 60;
		}
		-- iTick;

		if (bDestroyOnce)
			StartCoroutine (Timer ());
		else {
			iSec = 0;
			iTick = 0;
		}
	}

	IEnumerator DestroyThis(){
		yield return new WaitForSeconds(2.0f);
		Destroy (gameObject);
	}
}
